﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using BUS;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyDiemSV
{
    public partial class QLDiemThi : System.Web.UI.Page
    {
        ketnoi kn = new ketnoi();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (IsPostBack == false)
                {
                    string magiangvien = "GV01";
                    DataTable dt = BUS.QLDiemTrenLop.HienThiTen(magiangvien);
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataValueField = "TenMonHoc";
                    DropDownList1.DataTextField = "TenMonHoc";
                    DropDownList1.DataBind();

                }
            }
        }
        string masinhvien = "S01";
        private void load()
        {
            GridView1.DataSource = BUS.QLDiemThi.xemdiemthi(masinhvien);
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox txtmasv = (TextBox)GridView1.FooterRow.FindControl("txtmasv");
            TextBox txthoten = (TextBox)GridView1.FooterRow.FindControl("txthoten");
            TextBox txtdiem1 = (TextBox)GridView1.FooterRow.FindControl("txtdiem1");
            TextBox txtdiem2 = (TextBox)GridView1.FooterRow.FindControl("txtdiem2");
            string masv = txtmasv.Text;
            string hoten = txthoten.Text;
            float diem1 = float.Parse(txtdiem1.Text);
            float diem2 = float.Parse(txtdiem2.Text);
            string kq = kn.update("insert into DiemThi values('" + masv + "','" + hoten + "','" + diem1 + "','" + diem2 + "')");
            //string kq=BUS.QLDiemThi.update(masinhvien)
            if (kq != "")
            {
                Response.Write("<script>alert('ban them thanh cong');</script");
                load();

            }
            else
            {
                Response.Write("<script>alert('ban them that bai');</script");
            }

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string masv = e.Values["MaSinhVien"].ToString();
            string kq = kn.update("delete from DiemThi where MaSinhVien = " + masv);
            if (kq != "")
            {
                Response.Write("<script>alert('ban xoa thanh cong');</script");
                load();

            }
            else
            {
                Response.Write("<script>alert('ban xoa that bai');</script");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            load();
            
        }
        static string diemchu;
        public string diemtinchi(float diem)
        {
            if (diem < 4&&diem>=0)
            {
                diemchu = "F";
            }
            else if (diem >= 4 && diem < 5.5)
            {
                diemchu = "D";
            }
            else if(diem>=5.5&&diem<7)
            {
                diemchu = "C";
            }
            else if (diem >= 7 && diem < 8.5)
            {
                diemchu = "B";
            }
            else 
            {
                diemchu = "A";
            }
            return diemchu;
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string masv = e.NewValues["MaSinhVien"].ToString();
            string mamonhoc="M01";
            float diem1 = float.Parse(e.NewValues["DiemThiLan1"].ToString());
            float diem2 = float.Parse(e.NewValues["DiemThiLan2"].ToString());
            string kq = BUS.QLDiemThi.update(masv, diem1, diem2).ToString();
            float dtb = float.Parse(BUS.QLDiemThi.LayDiemTrungBinh(mamonhoc, masv).ToString());
            float diemtbc;
            if (diemchu == "F")
            {
                diemtbc = (dtb + diem2 * 2) / 3;
            }
            else
            {
                diemtbc = (dtb + diem1 * 2) / 3;
            }
            string update = BUS.QLDiemThi.updatediemlan1(masv, mamonhoc, diemtbc);
            diemchu=diemtinchi(diemtbc);
            string uddiemtinchi = BUS.QLDiemThi.UpdateDiemTinChi(masv, mamonhoc,diemchu);
            if (kq != "")
            {
                Response.Write("<script>alert('ban update thanh cong');</script");
                GridView1.EditIndex = -1;
                load();
            }
            else
            {
                Response.Write("<script>alert('ban update that bai');</script");
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            load();
        }

        protected void Tìm_Click(object sender, EventArgs e)
        {

        }

    }
}